'Copyright 2006 James Downey

Imports FetchXML
Imports FetchXMLBuilder.MetadataServiceSdk

Public Class frmCondition

    Private Condition As Condition
    Private Entity As EntityMetadata

    Public Sub New(ByVal Condition As Condition, ByVal Entity As EntityMetadata)
        InitializeComponent()
        Me.Condition = Condition
        Me.Entity = Entity
    End Sub

    Private Sub frmCondition_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadAttributes()
        If Not Condition.Attribute Is Nothing AndAlso Condition.Attribute.Length > 0 Then
            Me.lstAttributes.SelectedItem = Me.lstAttributes.Items.Item(Me.lstAttributes.FindString(Condition.Attribute))
        End If
        Me.lstOperator.SelectedItem = Me.lstOperator.Items.Item(Me.lstOperator.FindString(Me.Condition.ConditionOperator))
        Me.txtAlias.Text = Me.Condition.AttributeAlias
        Dim value As String
        For Each value In Me.Condition.Values
            Me.txtValues.Text &= value & vbCrLf
        Next
    End Sub

    Private Sub LoadAttributes()
        Me.lstAttributes.Items.Clear()
        Dim i As Integer
        For i = 0 To Entity.Attributes.Length - 1
            lstAttributes.Items.Add(Entity.Attributes(i).Name)
        Next
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If Me.lstAttributes.SelectedIndex = -1 Then
            Windows.Forms.MessageBox.Show("Select an attribute.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        Dim lines As New ArrayList
        Dim line As String
        Dim i As Integer
        For i = 0 To Me.txtValues.Lines.Length - 1
            line = Trim(Me.txtValues.Lines(i))
            If Trim(line).Length > 0 Then
                lines.Add(line)
            End If
        Next

        Select Case FetchXML.Condition.GetValueType(Me.lstOperator.SelectedItem)
            Case FetchXML.ValueType.NoValue
                If Trim(lines.Count) > 0 Then
                    Windows.Forms.MessageBox.Show("The selected operator does not take a value.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Exit Sub
                End If
            Case FetchXML.ValueType.OneValue
                If Not lines.Count > 0 Then
                    Windows.Forms.MessageBox.Show("The selected operator requires a value.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Exit Sub
                End If
                If lines.Count > 1 Then
                    Windows.Forms.MessageBox.Show("The selected operator takes only a single value.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Exit Sub
                End If
            Case FetchXML.ValueType.TwoValues
                If Not lines.Count = 2 Then
                    Windows.Forms.MessageBox.Show("The selected operator requires two and only two values.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Exit Sub
                End If
            Case FetchXML.ValueType.MultipleValues
                If Not lines.Count > 0 Then
                    Windows.Forms.MessageBox.Show("The selected operator requires at least one value.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Exit Sub
                End If
        End Select

        Me.Condition.Attribute = Me.lstAttributes.SelectedItem
        Me.Condition.ConditionOperator = Me.lstOperator.SelectedItem
        Me.Condition.AttributeAlias = Me.txtAlias.Text
        Me.Condition.Values.Clear()
        For Each line In lines
            Me.Condition.Values.Add(line)
        Next
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub


End Class